﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TS2015.Models;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Text;
using System.Data.OleDb;
using System.Data;


namespace TS2015.Areas.Admin.Controllers
{
    public class SearchController : Controller
    {
        TS2015Context db = new TS2015Context();
        // GET: Admin/Search
        public ActionResult Index()
        {
            //if (!CheckRole.IsRole("search_index", Session["Function"] as string[]))
            //    return Redirect("~/accessdenied.html");

            return View();
        }
        #region Search Candidate
        //[HttpGet]
        public ActionResult SearchCandidate()
        {
            //if (!CheckRole.IsRole("search_candidate", Session["Function"] as string[]))
            //    return Redirect("~/accessdenied.html");

            //Database.SetInitializer<TS2015Context>(null);
            // do du lieu len dropdownlist
            ViewBag.TruongTHPT = db.TbTruongTHPTs.ToList();
            ViewBag.DanToc = db.TbDanTocs.ToList();
            ViewBag.ChuyenNganh = db.TbChuyenNganhs.ToList();
            ViewBag.Truong = db.TbTruongTHPTs.ToList();
            ViewBag.Nganh = db.TbNganhs.ToList();
            //ViewBag.Truong = new SelectList(db.TbTruongTHPTs.ToList(), "IDTruong", "TenTruong");
            ViewBag.KhuVuc = db.TbKhuVucs.ToList();
            ViewBag.XepLoaiHS = db.TbXepLoaiHS.ToList();
            ViewBag.DanToc = db.TbDanTocs.ToList();
            ViewBag.DoiTuongUuTien = db.TbDoiTuongUuTiens.ToList();
            //ViewBag.KhoiThi = db.TbKhoiThis.ToList();
            //ViewBag.HeDaoTao = db.TbHeDaoTaos.ToList();
            ViewBag.Tinh = new SelectList(db.TbTinhs.ToList(), "MaTinh", "TenTinh");
            ViewBag.KhoiThi = new SelectList(db.TbKhoiThis.ToList(), "MaKhoiThi", "TenKhoiThi");
            ViewBag.HeDaoTao = new SelectList(db.TbHeDaoTaos.ToList(), "MaHeDaoTao", "TenHeDaoTao");
            
            if (Request.IsAjaxRequest())
                return PartialView();
            return View();
        }
        public JsonResult GetListCandidate(string searchID)
        {
            //if (!CheckRole.IsRole("search_candidate", Session["Function"] as string[]))
            //    return Json("", JsonRequestBehavior.AllowGet);

            var ListCandidates = from c in db.TbThiSinhs
                                 select c;
            ListCandidates = ListCandidates.Where(m => m.MaTS.Contains(searchID));

            return Json(ListCandidates, JsonRequestBehavior.AllowGet);
        }
        [HttpPost]
        [ActionName("SearchCandidate")]
        public ActionResult POST_SearchCandidate(TbThiSinh item, string MaTinh, string MaTinh2, string IDTruong, string HDT, string MaKhoiThi, string MaNganh)
        {
            //if (!CheckRole.IsRole("search_candidate", Session["Function"] as string[]))
            //    return Redirect("~/accessdenied.html");

            ViewBag.TruongTHPT = db.TbTruongTHPTs.ToList();
            ViewBag.DanToc = db.TbDanTocs.ToList();
            ViewBag.ChuyenNganh = db.TbChuyenNganhs.ToList();
            ViewBag.Truong = db.TbTruongTHPTs.ToList();
            ViewBag.Nganh = db.TbNganhs.ToList();
            //ViewBag.Truong = new SelectList(db.TbTruongTHPTs.ToList(), "IDTruong", "TenTruong");
            ViewBag.KhuVuc = db.TbKhuVucs.ToList();
            ViewBag.XepLoaiHS = db.TbXepLoaiHS.ToList();
            ViewBag.DanToc = db.TbDanTocs.ToList();
            ViewBag.DoiTuongUuTien = db.TbDoiTuongUuTiens.ToList();
            //ViewBag.KhoiThi = db.TbKhoiThis.ToList();
            //ViewBag.HeDaoTao = db.TbHeDaoTaos.ToList();
            ViewBag.Tinh = db.TbTinhs.ToList();
            ViewBag.KhoiThi = db.TbKhoiThis.ToList();
            ViewBag.HeDaoTao = db.TbHeDaoTaos.ToList();
            //string mats, hodem, ten, tinh, huyen, diachi, cmnd, tinh2, sbd, dt, chuyennganh, khoithi, nganh;
            //int truongpt, hdt;
            var ListCandidates = from c in db.TbThiSinhs select c;
            #region Test
            //if (!String.IsNullOrEmpty(item.MaTS) || !String.IsNullOrEmpty(item.HoDem) || !String.IsNullOrEmpty(item.Ten) || 
            //    !String.IsNullOrEmpty(MaTinh) || !String.IsNullOrEmpty(item.MaHuyen) || !String.IsNullOrEmpty(item.DiaChi) || 
            //    !String.IsNullOrEmpty(item.DiaChi) || !String.IsNullOrEmpty(item.CMND) || !String.IsNullOrEmpty(IDTruong) || 
            //    !String.IsNullOrEmpty(item.SBD) || !String.IsNullOrEmpty(item.SoDienThoai)|| !String.IsNullOrEmpty(HDT) || 
            //    !String.IsNullOrEmpty(item.MaChuyenNganh))
            //{
            //     ListCandidates = (from c in db.TbThiSinhs
            //                         join d in db.TbHuyens on c.MaHuyen equals d.MaHuyen
            //                         join e in db.TbTinhs on d.MaTinh equals e.MaTinh
            //                         join f in db.TbChuyenNganhs on c.MaChuyenNganh equals f.MaChuyenNganh
            //                         join g in db.TbNganhs on f.IDNganh equals g.IDNganh
            //                         join h in db.TbKhoiThis on g.MaKhoiThi equals h.MaKhoiThi
            //                         where (c.MaTS.Contains(item.MaTS) || c.MaTS.Equals(""))
            //                         && (c.HoDem.Contains(item.HoDem) || c.HoDem.Equals(""))
            //                         &&
            //                         (c.Ten.Contains(item.Ten) || c.Ten.Equals("")) &&
            //                         (d.MaTinh == MaTinh || d.MaTinh.Equals("")) &&
            //                         (c.MaHuyen.Equals(item.MaHuyen) || c.MaHuyen.Equals("")) &&
            //                         (c.DiaChi.Contains(item.DiaChi) || c.DiaChi.Equals("")) &&
            //                         (c.CMND.Equals(item.CMND) || c.CMND.Equals("")) &&
            //                         (d.MaTinh.Equals(MaTinh2) || d.MaTinh.Equals("")) &&
            //                         (c.IDTruong.Equals(item.IDTruong) || c.IDTruong.Equals("")) &&
            //                         (c.SBD.Equals(item.SBD) || c.SBD.Equals("")) &&
            //                         (c.SoDienThoai.Contains(item.SoDienThoai) || c.SoDienThoai.Equals("")) &&
            //                         (c.MaHeDaoTao.Equals(item.MaHeDaoTao) || c.MaHeDaoTao.Equals("")) &&
            //                         (g.MaKhoiThi.Equals(MaKhoiThi) || g.MaKhoiThi.Equals("")) &&
            //                         (f.IDNganh.Equals(MaNganh) || f.IDNganh.Equals("")) &&
            //                         (f.MaChuyenNganh.Equals(item.MaChuyenNganh) || f.MaChuyenNganh.Equals(""))
            //                         select c);
            //    return View("_Result", ListCandidates);
            //return PartialView();
            //}
            #endregion
            if (!String.IsNullOrEmpty(item.MaTS))
            {
                ListCandidates = ListCandidates.Where(m => m.MaTS.Contains(item.MaTS));
            }
            if (!String.IsNullOrEmpty(item.HoDem))
            {
                ListCandidates = ListCandidates.Where(m => m.HoDem.Contains(item.HoDem));

            }
            if (!String.IsNullOrEmpty(item.Ten))
            {
                ListCandidates = ListCandidates.Where(m => m.Ten.Contains(item.Ten));
            }
            if (!String.IsNullOrEmpty(MaTinh))
            {
                ListCandidates = (from c in db.TbThiSinhs
                                  join d in db.TbHuyens on c.MaHuyen equals d.MaHuyen
                                  join e in db.TbTinhs on d.MaTinh equals e.MaTinh
                                  where d.MaTinh == MaTinh
                                  select c);
            }
            if (!String.IsNullOrEmpty(item.MaHuyen))
            {
                ListCandidates = ListCandidates.Where(m => m.MaHuyen == item.MaHuyen);
            }
            if (!String.IsNullOrEmpty(item.DiaChi))
            {
                ListCandidates = ListCandidates.Where(m => m.DiaChi.Contains(item.DiaChi));
            }
            if (!String.IsNullOrEmpty(item.CMND))
            {
                ListCandidates = ListCandidates.Where(m => m.CMND.Contains(item.CMND));
            }
            if (!String.IsNullOrEmpty(IDTruong))
            {
                int ma = int.Parse(IDTruong);
                ListCandidates = ListCandidates.Where(m => m.IDTruong == ma);
            }
            if (!String.IsNullOrEmpty(item.SBD))
            {
                ListCandidates = ListCandidates.Where(m => m.SBD.Contains(item.SBD));
            }
            if (!String.IsNullOrEmpty(item.SoDienThoai))
            {
                ListCandidates = ListCandidates.Where(m => m.SoDienThoai.Contains(item.SoDienThoai));
            }
            if (!String.IsNullOrEmpty(HDT))
            {
                int mahdt = int.Parse(HDT);
                ListCandidates = ListCandidates.Where(m => m.MaHeDaoTao == mahdt);
            }
            if (!String.IsNullOrEmpty(MaNganh))
            {
                int manganh = int.Parse(MaNganh);
                ListCandidates = (from c in db.TbThiSinhs
                                  join d in db.TbChuyenNganhs on c.MaChuyenNganh equals d.MaChuyenNganh
                                  join e in db.TbNganhs on d.IDNganh equals e.IDNganh
                                  where d.IDNganh == manganh
                                  select c);
            }
            if (!String.IsNullOrEmpty(item.MaChuyenNganh))
            {
                ListCandidates = ListCandidates.Where(m => m.MaChuyenNganh == item.MaChuyenNganh);
            }

            return PartialView("_ResultCandidate", ListCandidates);
        }
        #endregion
        #region Search HighSchool
        public ActionResult SearchHighSchool()
        {
            //Database.SetInitializer<TS2015Context>(null);
            // do du lieu len dropdownlist
            //if (!CheckRole.IsRole("search_high_school", Session["Function"] as string[]))
            //    return Redirect("~/accessdenied.html");

            ViewBag.KhuVuc = db.TbKhuVucs.ToList();
            ViewBag.Tinh = new SelectList(db.TbTinhs.ToList(), "MaTinh", "TenTinh");
            ViewBag.KhuVuc = new SelectList(db.TbKhuVucs.ToList(), "MaKhuVuc", "TenKhuVuc");
            
            if (Request.IsAjaxRequest())
                return PartialView();
            return View();
        }
        [HttpPost]
        [ActionName("SearchHighSchool")]
        public ActionResult POST_SearchHighShool(TbTruongTHPT item)
        {
            //if (!CheckRole.IsRole("search_high_school", Session["Function"] as string[]))
            //    return Redirect("~/accessdenied.html");

            ViewBag.KhuVuc = db.TbKhuVucs.ToList();
            ViewBag.Tinh = db.TbTinhs.ToList();
            
            var ListHighSchools = from c in db.TbTruongTHPTs select c;
            
            if (!String.IsNullOrEmpty(item.MaTruong))
            {
                ListHighSchools = ListHighSchools.Where(m => m.MaTruong.Contains(item.MaTruong));
            }
            if (!String.IsNullOrEmpty(item.TenTruong))
            {
                ListHighSchools = ListHighSchools.Where(m => m.TenTruong.Contains(item.TenTruong));

            }
            if (!String.IsNullOrEmpty(item.MaTinh))
            {

                ListHighSchools = ListHighSchools.Where(m => m.MaTinh.Contains(item.MaTinh));
            }
            if (!String.IsNullOrEmpty(item.DiaChi))
            {
                ListHighSchools = ListHighSchools.Where(m => m.DiaChi.Contains(item.DiaChi));
            }
            if (!String.IsNullOrEmpty(item.MaKhuVuc))
            {
                ListHighSchools = ListHighSchools.Where(m => m.MaKhuVuc.Contains(item.MaKhuVuc));
            }

            return PartialView("_ResultHighSchool", ListHighSchools);
        }
        #endregion
        #region Search University
        public ActionResult SearchUniversity()
        {
            //if (!CheckRole.IsRole("search_university", Session["Function"] as string[]))
            //    return Redirect("~/accessdenied.html");

            if (Request.IsAjaxRequest())
                return PartialView();
            return View();
        }
        [HttpPost]
        [ActionName("SearchUniversity")]
        public ActionResult POST_SearchUniversity(TbTruongDH item)
        {
            //if (!CheckRole.IsRole("search_university", Session["Function"] as string[]))
            //    return Redirect("~/accessdenied.html");

            var ListUniversitys = from c in db.TbTruongDHs select c;

            if (!String.IsNullOrEmpty(item.MaTruongDH))
            {
                ListUniversitys = ListUniversitys.Where(m => m.MaTruongDH.Contains(item.MaTruongDH));
            }
            if (!String.IsNullOrEmpty(item.TenTruongDH))
            {
                ListUniversitys = ListUniversitys.Where(m => m.TenTruongDH.Contains(item.TenTruongDH));

            }
            if (!String.IsNullOrEmpty(item.DiaChi))
            {
                ListUniversitys = ListUniversitys.Where(m => m.DiaChi.Contains(item.DiaChi));
            }

            return PartialView("_ResultUniversity", ListUniversitys);
        }
        #endregion
    }
}